<template>
  <a-modal
          title="新增用户"
          :width="640"
          :visible="visible"
          :confirmLoading="loading"
          @ok="ok"
          @cancel="() => { $emit('cancel') }"
  >
    <a-spin :spinning="loading">
      <a-form :form="form" v-bind="formLayout" key="FormAdd">
        <a-form-item v-show="model && model.id > 0" label="主键ID">
          <a-input v-decorator="['id', { initialValue: 0 }]" disabled/>
        </a-form-item>
        <a-form-item label="用户名">
          <a-input v-decorator="['username', {rules: [{required: true, message: '不能为空！'}]}]"/>
        </a-form-item>
        <a-form-item label="用户账户">
          <a-input v-decorator="['userAccount', {rules: [{required: true, message: '不能为空！'}]}]"
                   :disabled="model && model.id > 0"/>
        </a-form-item>
        <a-form-item v-if="model && model.id > 0" label="用户密码">
          <a-input-password v-decorator="['userPassword', {rules: [{required: true, message: '不能为空！'}]}]"/>
        </a-form-item>
        <a-form-item label="性别">
          <a-select
                  v-decorator="['gender', {rules: [{ required: true, message: '不能为空!' }] }]"
                  placeholder=""
          >
            <a-select-option :value="1">
              男
            </a-select-option>
            <a-select-option :value="0">
              女
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="电话">
          <a-input v-decorator="['phone', {rules: [{required: true, message: '不能为空！'}]}]"/>
        </a-form-item>
        <a-form-item label="邮件">
          <a-input v-decorator="['email', {rules: [{required: true, message: '不能为空！'}]}]"/>
        </a-form-item>
        <a-form-item v-if="model && model.id > 0" label="状态">
          <a-select
                  v-decorator="['userStatus', {rules: [{ required: true, message: '不能为空!' }] }]"
                  placeholder=""
          >
            <a-select-option :value="0">
              有效
            </a-select-option>
            <a-select-option :value="1">
              无效
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="角色">
          <!--用户角色 0 - 普通用户 1 - 管理员-->
          <a-select
                  v-decorator="['userRole', {rules: [{ required: true, message: '不能为空!' }] }]"
                  placeholder=""
          >
            <a-select-option :value="0">
              普通用户
            </a-select-option>
            <a-select-option :value="1">
              管理员
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="星球编号">
          <a-input v-decorator="['planetCode', {rules: [{required: true, message: '不能为空！'}]}]"
                   :disabled="model && model.id > 0"/>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import pick from 'lodash.pick'

  // 表单字段
  const fields = ['id', 'username', 'userAccount', 'userPassword', 'gender', 'phone', 'email', 'userStatus', 'userRole', 'planetCode',]

  export default {
    props: {
      visible: {
        type: Boolean,
        required: true
      },
      loading: {
        type: Boolean,
        default: () => false
      },
      // model: {
      //   type: Object,
      //   default: () => null
      // }
    },
    data() {
      this.formLayout = {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 7 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 13 }
        }
      }
      return {
        form: this.$form.createForm(this),
        model: null
      }
    },
    methods: {
      init(model) {
        this.model = model
        // 防止表单未注册
        fields.forEach(v => this.form.getFieldDecorator(v))
        // 当 model 发生改变时，为表单设置值
        this.form.setFieldsValue(pick(model, fields))
      },
      ok() {
        console.log('ok')
        this.$emit('ok')
      }
    }
  }
</script>
